// pages/order/index.js
const app = getApp()
Page({
    data: {
        url: app.globalData.url,
        orderList: [],
        total: undefined,
        address: undefined,
        expire: undefined,
        index: undefined
    },
    onShow() {
        // 获取登录用户信息
        const user = wx.getStorageSync("user")
        if (user) {
            this.setData({ user })
        }
    },
    onLoad(options) {
        if (options) {
            this.setData({
                orderList: JSON.parse(options.list),
                total: options.total,
                address: JSON.parse(options.list)[0].address
            })
            if (options.type) {
                this.setData({ type: options.type })
                let state;
                let orders = this.data.orderList[0]
                if (orders.state === 0) {
                    state = "待付款"
                    let expire = new Date(orders.createDate).getTime() + 15 * 60 * 1000 - new Date().getTime()
                    this.setData({ expire })
                    let index = setInterval(() => {
                        this.countDown()
                    }, 1000)
                    this.setData({ index })
                } else if (orders.state === 1) {
                    state = "已付款"
                }
                wx.setNavigationBarTitle({
                    title: state,
                })
            }
        }
    },
    // 为订单选择地址
    address() {
        wx.navigateTo({
            url: '/pages/address/index',
        })
    },
    // 格式化倒计时时间
    timeFormatter(time) {
        if (time < 0) {
            return "";
        } else {
            time = time / 1000;
            let second = Math.floor(time % 60)
            let min = Math.floor(time / 60);  // 分
            return (min >= 10 ? min : "0" + min) + "分" +
                (second >= 10 ? second : "0" + second) + "秒"
        }
    },
    // 倒计时
    countDown() {
        let expire = this.data.expire - 1000
        if (expire < 0) {
            // 时间到 订单自动取消
            const that = this
            wx.request({
                url: that.data.url + '/kill/cancle',
                data: {
                    id: this.data.orders.id,
                },
                success(res) {
                    if (res.data.code == 1) {
                        clearInterval(that.data.index)
                    }
                }
            })
        }
        this.setData({ expire })
        this.setData({ countDown: this.timeFormatter(expire) })
    },
    // 重新选购加入购物车
    addCar() {
        wx.showLoading({
            title: '请稍后...',
        })
        let goods = this.data.orderList[0].goods
        const that = this
        wx.request({
            url: that.data.url + '/car/add',
            data: {
                uid: that.data.user.id,
                gid: goods.id
            },
            success(res) {
                if (res.data.code == 1) {
                    wx.hideLoading({
                        success: (res) => {
                            wx.showToast({
                                title: '添加成功',
                                success() {
                                    wx.reLaunch({
                                        url: '/pages/car/index',
                                    })
                                }
                            })
                        },
                    })

                } else {
                    wx.showModal({
                        title: '错误',
                        content: res.data.msg,
                        success(res) {
                            if (res.confirm) { } else if (res.cancel) { }
                        }
                    })
                }
            }
        })
    },
    // 立即支付，支付成功状态为1 支付失败状态为 0 
    pay() {
        const that = this
        let url =
            wx.showModal({
                title: '提示',
                content: '模拟支付',
                success(res) {
                    if (res.confirm) {
                        that.data.orderList.forEach(order => {
                            // 支付订单提交到后端
                            let url = that.data.url + "/kill/pay"
                            wx.request({
                                url: url,
                                data: { id: order.id },
                                success(res) {
                                    if (res.data.code === 1) {
                                        wx.showToast({
                                            title: '支付成功',
                                            success(res) {
                                                wx.switchTab({
                                                    url: '/pages/mine/index',
                                                })
                                            }
                                        })
                                    }
                                }
                            })
                        })
                    } else if (res.cancel) {
                        that.data.orderList.forEach(order => {
                            // 取消支付，关闭页面，订单不变
                            wx.showToast({
                                title: '取消支付',
                                success(res) {
                                    wx.switchTab({
                                        url: '/pages/car/index',
                                    })
                                }
                            })
                        })
                    }
                }
            })
    },
    formatDate(date) {
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
    },
    onShow() {
    },
})